<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>08-简单动画函数封装</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }

        span {
            /* 动画一定要加定位 */
            position: absolute;
            left: 0;
            top: 200px;
            display: block;
            width: 150px;
            height: 150px;
            background-color: purple;
        }
    </style>
</head>
<body>
    <button>开启</button>
    <div>一路向北</div>
    <span>龙卷风</span>
    <script>
        /* 
          需求:
                1. span 自动往右移动，某个位置后停止
                2. 点击按钮后，div 往右移动，某个位置后自动停止。
            解决方案：写一个函数，元素和距离作为参数。
        */
       let btn = document.querySelector('button');
       let div = document.querySelector('div');
       let span = document.querySelector('span');

        move(span,500);
       btn.addEventListener('click',function(){
           move(div,600);
       });

       function move(ele,target){
           let timer = setInterval(()=>{
               let left =ele.offsetLeft;
               if(left >= target){
                   clearInterval(timer);
                   return;
               }
               ele.style.left = left + 20 + 'px';
           },500);
       }
    </script>
</body>
</html>